﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FisApp.enums;
using FisApp.utility;
using System.Data.SqlClient;

namespace FisApp
{
    public partial class FrmElSarac : Form
    {
        #region Variables...
        private string elSaracId;
        private string sqlCommandType = SqlCommandType.INSERT;

        public string elSaracKodu
        {
            get
            {
                return txtElSaracKodu.Text;
            }
            set
            {
                txtElSaracKodu.Text = value;
            }
        }

        public string elSaracAciklama
        {
            get
            {
                return txtElSaracAciklama.Text;
            }
            set
            {
                txtElSaracAciklama.Text = value;
            }
        }

        public DataTable ElSaraclar
        {
            get
            {
                return (DataTable)gcElSarac.DataSource;
            }

            set
            {
                gcElSarac.DataSource = value;
            }
        }

        public DataRow SelectedRow
        {
            get
            {
                return gvElSarac.GetDataRow(gvElSarac.FocusedRowHandle);
            }
        }
        #endregion

        #region Constructor...
        public FrmElSarac()
        {
            InitializeComponent();
        }
        #endregion

        #region Methods...
        private void clearForm()
        {
            elSaracId = null;
            elSaracKodu = null;
            elSaracAciklama = null;
            btnVazgec.Visible = false;
            sqlCommandType = SqlCommandType.INSERT;
        }

        private void LoadElSaraclar()
        {
            try
            {
                string query = "SELECT * FROM elsarac";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                    ElSaraclar = ds.Tables[0];
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }
        #endregion

        #region Events...
        private void FrmElSarac_Load(object sender, EventArgs e)
        {
            LoadElSaraclar();
        }

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(elSaracKodu))
            {
                FrmMessage.showDialog("Lütfen el saraç kodu giriniz.", MessageTypes.Warning);
                return;
            }

            if (string.IsNullOrEmpty(elSaracAciklama))
            {
                FrmMessage.showDialog("Lütfen el saraç açıklama giriniz.", MessageTypes.Warning);
                return;
            }

            try
            {
                if (sqlCommandType.Equals(SqlCommandType.INSERT))
                {
                    string command = "INSERT INTO elsarac (elsarac_kodu, elsarac_aciklama) VALUES (@elsarac_kodu, @elsarac_aciklama)";

                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@elsarac_kodu", elSaracKodu));
                    list.Add(DBUtils.GetParameter("@elsarac_aciklama", elSaracAciklama));
                    DBUtils.ExecuteSQLCommand(command, list);
                }
                else if (sqlCommandType.Equals(SqlCommandType.UPDATE))
                {
                    string command = "UPDATE elsarac SET elsarac_kodu=@elsarac_kodu, elsarac_aciklama=@elsarac_aciklama WHERE elsarac_id=@elsarac_id";

                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@elsarac_kodu", elSaracKodu));
                    list.Add(DBUtils.GetParameter("@elsarac_aciklama", elSaracAciklama));
                    list.Add(DBUtils.GetParameter("@elsarac_id", elSaracId));
                    DBUtils.ExecuteSQLCommand(command, list);
                }
                LoadElSaraclar();
                clearForm();
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Kayıt işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void repbtnEdit_Click(object sender, EventArgs e)
        {
            elSaracId = SelectedRow["elsarac_id"].ToString();
            elSaracKodu = SelectedRow["elsarac_kodu"].ToString();
            elSaracAciklama = SelectedRow["elsarac_aciklama"].ToString();
            sqlCommandType = SqlCommandType.UPDATE;
            btnVazgec.Visible = true;
        }

        private void repbtnDelete_Click(object sender, EventArgs e)
        {
            elSaracId = SelectedRow["elsarac_id"].ToString();

            try
            {
                if (FrmMessage.ShowDialogResult(SelectedRow["elsarac_kodu"].ToString() + " kod'lu el saracı bilgisini silmek istediğinize emin misiniz ?", MessageTypes.Question) == DialogResult.Yes)
                {
                    string command = "DELETE FROM elsarac WHERE elsarac_id=@elsarac_id";

                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@elsarac_id", elSaracId));
                    DBUtils.ExecuteSQLCommand(command, list);

                    LoadElSaraclar();
                    clearForm();
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.ToString().Contains("-2146232060"))
                    FrmMessage.showDialog(General.ForeignKeyExceptionMessage, MessageTypes.Error);
                else
                    FrmMessage.showDialog("Silme işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);

                //FrmMessage.showDialog("Silme işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void btnVazgec_Click(object sender, EventArgs e)
        {
            clearForm();
        }

        private void FrmElSarac_Activated(object sender, EventArgs e)
        {
            FrmElSarac_Load(null, null);
        }
        #endregion

    }
}
